//
// Created by 刘禹晨 on 16/5/25.
// Copyright (c) 2016 刘禹晨. All rights reserved.
//

#import "NodeCell.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import "BLEManager.h"
#import "SocketManager.h"
#import "CocoaAsyncSocket/GCDAsyncSocket.h"
#import "Mode.h"
#import "CBHelper/CBHelper.h"

#define kW [UIScreen mainScreen].bounds.size.width
#define kH [UIScreen mainScreen].bounds.size.width

static NSString *armCommand =    @"11SSSNSNNNN&F";
static NSString *disarmCommand = @"11SNSSSNNNN&F";

@interface NodeCell() 

{
    UILabel *_nodeTypeLabel;
    UILabel *_nodeAddress;
    UILabel *_nodeMac;
    UILabel *_nodeState;
    UIImageView *_typeImage;
    UIButton *_uiButton;
    BLEManager *_bleManger;
    CBPeripheral *_cbPeripheral;
    CBCharacteristic *_writeCharacteristic;
    GCDAsyncSocket *_currectSocket;
    Mode *_mode;
    UIImage *armImage;
    UIImage *disArmImage;

}


@end

@implementation NodeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        [self setupAutoHeightWithBottomView:_typeImage bottomMargin:5];
    }
    return self;
}

-(void)setup
{
    [self initTypeImage];
    [self initButton];
    [self initNodeStateLabel];
    [self initTypeLabel];
    [self initNodeAddressLabel];
    //[self initNodeMacLabel];
    _bleManger = [BLEManager instance];
    _cbPeripheral = _bleManger.currentPeripheral;
    _writeCharacteristic = _bleManger.cbCharacteristic;
    _currectSocket = [SocketManager instance].socket;
    _mode = [Mode instance];
    armImage = [CBHelper imageWithColor:[[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f] size:CGSizeMake(self.contentView.bounds.size.width/5.5,self.contentView.bounds.size.height-8)];
    disArmImage = [CBHelper imageWithColor:[[UIColor new] initWithRed:254.0f/255.0f green:196.0f/255.0f blue:46.0f/255.0f alpha:1.0f] size:CGSizeMake(self.contentView.bounds.size.width/5.5,self.contentView.bounds.size.height-8)];

}


//界面初始化方法
#pragma mark - UIInit Methods
-(void)initTypeImage
{
    _typeImage = [[UIImageView alloc]init];
    _typeImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_typeImage];
    
    _typeImage.sd_layout.heightIs(40).widthIs(40).topSpaceToView(self.contentView,8)
                        .leftSpaceToView(self.contentView,5);
}

-(void)initTypeLabel
{
    _nodeTypeLabel = [[UILabel alloc]init];
    _nodeTypeLabel.font = [UIFont systemFontOfSize:17];
    _nodeTypeLabel.textColor = [UIColor blackColor];
    _nodeTypeLabel.text = @"点类型";
    CGFloat height =  [_nodeTypeLabel.text sizeWithAttributes:@{NSFontAttributeName : _nodeTypeLabel.font}].height;
    //CGFloat width =  [_nodeTypeLabel.text sizeWithAttributes:@{NSFontAttributeName : _nodeTypeLabel.font}].width;
    [self.contentView addSubview:_nodeTypeLabel];
    _nodeTypeLabel.sd_layout.heightIs(height).leftSpaceToView(_typeImage,12).topEqualToView(_typeImage).rightSpaceToView(_nodeState,5);
}

-(void)initNodeAddressLabel
{
    _nodeAddress = [[UILabel alloc]init];
    _nodeAddress.font = [UIFont systemFontOfSize:12];
    _nodeAddress.textColor = [UIColor grayColor];
    _nodeAddress.text = @"点类型";
    CGFloat height =  [_nodeAddress.text sizeWithAttributes:@{NSFontAttributeName : _nodeAddress.font}].height;
    //CGFloat width =  [_nodeAddress.text sizeWithAttributes:@{NSFontAttributeName : _nodeAddress.font}].width;
    [self.contentView addSubview:_nodeAddress];
    _nodeAddress.sd_layout.topSpaceToView(_nodeTypeLabel,5).rightEqualToView(_nodeState).heightIs(height).leftEqualToView(_nodeTypeLabel);
}

//-(void)initNodeMacLabel
//{
//    _nodeMac = [[UILabel alloc]init];
//    _nodeMac.font = [UIFont systemFontOfSize:12];
//    _nodeMac.textColor = [UIColor grayColor];
//    _nodeMac.text = @"Mac地址: AA:AA:AA:AA:AA:AA:AA:AA";
//    CGFloat height = [_nodeMac.text sizeWithAttributes:@{NSFontAttributeName : _nodeMac.font}].height;
//    CGFloat width = [_nodeMac.text sizeWithAttributes:@{NSFontAttributeName : _nodeMac.font}].width;
//    [self.contentView addSubview:_nodeMac];
//    
//    _nodeMac.sd_layout.widthIs(width).heightIs(height)
//            .leftEqualToView(_nodeAddress).topSpaceToView(_nodeAddress,8);
//}

-(void)initButton
{
    _uiButton = [[UIButton alloc]init];
    //[_uiButton setTitle:@"布防" forState:UIControlStateNormal];
    //[_uiButton setBackgroundColor:[[UIColor new] initWithRed:255/255.0f green:115/255.0f blue:73/255.0f alpha:1.0f]];
    _uiButton.layer.masksToBounds = YES;
    _uiButton.layer.cornerRadius = 3.5;
    //[_uiButton addTarget:self action:@selector(writeValue:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_uiButton];
    
    _uiButton.sd_layout.widthIs(self.contentView.bounds.size.width/5.5).heightIs(self.contentView.bounds.size.height-8)
    .centerYEqualToView(self.contentView).rightSpaceToView(self.contentView,8);
}

-(void)initNodeStateLabel
{
    _nodeState = [[UILabel alloc]init];
    _nodeState.font = [UIFont systemFontOfSize:20];
    _nodeState.textColor = [UIColor grayColor];
    _nodeState.text = @"正常吗?";
    _nodeState.textAlignment = NSTextAlignmentCenter;
    _nodeState.backgroundColor = [UIColor grayColor];
    _nodeState.textColor = [UIColor whiteColor];
    _nodeState.layer.masksToBounds = YES;
    _nodeState.layer.cornerRadius = 3.5;
    //CGFloat height = [_nodeState.text sizeWithAttributes:@{NSFontAttributeName : _nodeState.font}].height;
    CGFloat width = [_nodeState.text sizeWithAttributes:@{NSFontAttributeName : _nodeState.font}].width;
    [self.contentView addSubview:_nodeState];
    
    _nodeState.sd_layout.widthIs(width).heightIs(_uiButton.bounds.size.height).rightSpaceToView(_uiButton,8)
                    .centerYEqualToView(self.contentView);
}





-(void)writeArmValue:(id)sender
{
    NSString *firstChar = @"#";
    NSString *nodeAddress = [firstChar stringByAppendingString:self.model.NodeNetworkAddress];
    NSString *nodeAddressAndMac = [nodeAddress stringByAppendingString:self.model.realMac];
    NSString *result = [nodeAddressAndMac stringByAppendingString:armCommand];
    NSLog(@"%@",result);
    NSData *bCharData = [result dataUsingEncoding:NSASCIIStringEncoding];
    if([_mode.mode isEqualToString:@"BLE"]){
        [_cbPeripheral writeValue:bCharData forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];
    }else{
        [_currectSocket writeData:bCharData withTimeout:5.0 tag:1];
    }
}

-(void)writeDisArmValue:(id)sender
{
    NSString *firstChar = @"#";
    NSString *nodeAddress = [firstChar stringByAppendingString:self.model.NodeNetworkAddress];
    NSString *nodeAddressAndMac = [nodeAddress stringByAppendingString:self.model.realMac];
    NSString *result = [nodeAddressAndMac stringByAppendingString:disarmCommand];
    NSLog(@"%@",result);
    NSData *bCharData = [result dataUsingEncoding:NSASCIIStringEncoding];
    if([_mode.mode isEqualToString:@"BLE"]){
        [_cbPeripheral writeValue:bCharData forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];
    }else{
        [_currectSocket writeData:bCharData withTimeout:5.0 tag:1];
    }
}

-(void)setButtonState:(BOOL)isArm
{
    if(isArm == true)
    {
        [_uiButton setTitle:@"撤防" forState:UIControlStateNormal];
        [_uiButton setBackgroundImage:disArmImage forState:UIControlStateNormal];
        [_uiButton addTarget:self action:@selector(writeDisArmValue:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [_uiButton setTitle:@"布防" forState:UIControlStateNormal];
        [_uiButton setBackgroundImage:armImage forState:UIControlStateNormal];
        [_uiButton addTarget:self action:@selector(writeArmValue:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)setModel:(ZigbeeModel *)model
{
    _model = model;
    if([_model.NodeType isEqualToString:@"E"])
    {
        [_typeImage setImage:[UIImage imageNamed:@"end"]];
        [_nodeTypeLabel setText:@"报警器"];
        _nodeAddress.text = [@"网络地址: " stringByAppendingString:_model.NodeNetworkAddress];
    }else if([_model.NodeType isEqualToString:@"R"])
    {
        [_typeImage setImage:[UIImage imageNamed:@"end"]];
        [_nodeTypeLabel setText:@"报警器"];
        _nodeAddress.text = [@"网络地址: " stringByAppendingString:_model.NodeNetworkAddress];
    } else
    {
        [_typeImage setImage:[UIImage imageNamed:@"coordinatior"]];
        [_nodeTypeLabel setText:@"网关"];
        _nodeAddress.text = [@"网络地址: " stringByAppendingString:_model.NodeNetworkAddress];
        _uiButton.hidden = YES;
    }
    //_nodeAddress.text = [@"网络地址:" stringByAppendingString:_model.NodeNetworkAddress];
    _nodeMac.text = [@"Mac地址:" stringByAppendingString:_model.NodeMac];
    if([model.isArm isEqualToString:@"N"] && [model.AlterState isEqualToString:@"R"]){
        _nodeState.text = @"未布防";
        _nodeState.backgroundColor = [[UIColor new] initWithRed:170/255.0f green:170/255.0f blue:176/255.0f alpha:1.0f];
        [self setButtonState:false];
    }else if([model.isArm isEqualToString:@"S"] && [model.AlterState isEqualToString:@"R"]){
        _nodeState.text = @"布防";
        _nodeState.backgroundColor = [[UIColor new] initWithRed:75/255.0f green:203/255.0f blue:38/255.0f alpha:1.0f];
        [self setButtonState:true];
    }else if([model.AlterState isEqualToString:@"A"]){
        _nodeState.text = @"报警";
        _nodeState.backgroundColor = [[UIColor new] initWithRed:255/255.0f green:60/255.0f blue:45/255.0f alpha:1.0f];
        [self setButtonState:false];
    }else{
        _nodeState.text = @"正常";
        _nodeState.backgroundColor = [[UIColor new] initWithRed:0.0/255.0f green:122.0/255.0f blue:255.0/255.0f alpha:1.0f];
        [self setButtonState:false];
    }
}


@end